package com.shengyijie.activity;

  
import com.shengyijie.activity.usertab.AttentionActivity; 
import com.shengyijie.activity.usertab.MessageActivity; 
import com.shengyijie.activity.usertab.UserCenterActivity;
import com.shengyijie.context.ContextApplication;  
import com.shengyijie.util.Utility;

import android.app.ActivityGroup;
import android.app.AlertDialog;  
import android.content.DialogInterface;
import android.content.Intent;  
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;  
import android.util.DisplayMetrics;
import android.view.KeyEvent; 
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window; 
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TextView;



public class UserTabActivity extends  ActivityGroup implements OnTabChangeListener{

	 public static UserTabActivity group;
	 Uri originalUri ;
	 Button right;
	 public static TabHost tabhost;
	 public static TabHost.TabSpec tabOne;
	 public static TabHost.TabSpec tabTwo;
	 public static TabHost.TabSpec tabThr;
	 public static TabHost.TabSpec tabFour;
	 public static TabHost.TabSpec tabFive;  
	 LinearLayout ll;
	 private ContextApplication app;
	 public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); 
	        setContentView(R.layout.main);
	        group=this;
	        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,     
                    R.layout.title1);
	        app=(ContextApplication)this.getApplication();
	        ContextApplication.activity=this;
	        right= (Button) UserTabActivity.this.findViewById(R.id.right); 
	        right.setVisibility(View.VISIBLE);
	        tabhost=(TabHost) this.findViewById(R.id.tabhost);
	        tabhost.setup(this.getLocalActivityManager());
	        tabhost.setBackgroundColor(Color.argb(150, 22, 70, 150));  
	        
	        LayoutInflater.from(this).inflate(R.layout.camera, tabhost.getTabContentView(), true); 
	   	    tabOne=tabhost.newTabSpec("home").setIndicator("").setContent(R.id.camera);
	   	    tabTwo=tabhost.newTabSpec("usercenter").setIndicator("").   
	        		setContent(new Intent(this, UserCenterActivity.class));
	   	    tabThr=tabhost.newTabSpec("attention").setIndicator(""). 
	        		setContent(new Intent(this, AttentionActivity.class)); 
	   	    tabFour=tabhost.newTabSpec("message").setIndicator(""). 
	        		setContent(new Intent(this, MessageActivity.class)); 
	        tabFive=tabhost.newTabSpec("exit").setIndicator("").setContent(R.id.camera);
	        
	        tabhost.addTab(tabOne);
	        tabhost.addTab(tabTwo);
	        tabhost.addTab(tabThr);
	        tabhost.addTab(tabFour);
	        tabhost.addTab(tabFive);
	        for(int i=0;i<5;i++){
	            View view;
	            view = tabhost.getTabWidget().getChildAt(0);
	        	view.setBackgroundResource(R.drawable.tab_home_selector);
	        	view = tabhost.getTabWidget().getChildAt(1);
	        	view.setBackgroundResource(R.drawable.tab_usercenter_selector);
	        	view = tabhost.getTabWidget().getChildAt(2);
	        	view.setBackgroundResource(R.drawable.tab_attention_selector);
	        	view = tabhost.getTabWidget().getChildAt(3);
	        	view.setBackgroundResource(R.drawable.tab_message_selector);
	        	view = tabhost.getTabWidget().getChildAt(4);
	        	view.setBackgroundResource(R.drawable.tab_exit_selector);
		     
	        }

	        DisplayMetrics dm = new DisplayMetrics();   
	        getWindowManager().getDefaultDisplay().getMetrics(dm);  
	        int height;
	        if(dm.densityDpi==DisplayMetrics.DENSITY_LOW){
	        	height = dm.heightPixels/9+4;
	        	tabhost.setPadding(0,0, 0, tabhost.getPaddingBottom()-4);
	        }else{
	        	tabhost.setPadding(0,0, 0, tabhost.getPaddingBottom()-5);
	        	height = dm.heightPixels/12+8;
	        }
	        for (int i =0; i < tabhost.getTabWidget().getChildCount(); i++) {
	             
	            tabhost.getTabWidget().getChildAt(i).getLayoutParams().height = height;
	       
	        }
            tabhost.setCurrentTab(1);
            ContextApplication.prePage=1;
            tabhost.setOnTabChangedListener(this);  
            
            tabhost.getTabWidget().getChildAt(4).setFocusable(false);
            tabhost.getTabWidget().getChildAt(4).setOnClickListener(new OnClickListener() {  
                public void onClick(View v) {   
                	 
                		new AlertDialog.Builder(UserTabActivity.this).setTitle("提示")
            			 .setMessage("您确定要退出生意街吗？")
            			 .setPositiveButton("确定", new DialogInterface.OnClickListener() {
            				public void onClick(DialogInterface dialog, int whichButton) {
            					Utility.clearUserSession(UserTabActivity.this);
            					UserTabActivity.this.finish();
            					MainActivity.tabhost.setCurrentTab(0);
            					ContextApplication.tabtype=0;
            				}
            			  }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
          				    public void onClick(DialogInterface dialog, int whichButton) {
       				     }
       			  })
            			  .show();  
                } 

            }); 
            
            tabhost.getTabWidget().getChildAt(0).setFocusable(false);
            tabhost.getTabWidget().getChildAt(0).setOnClickListener(new OnClickListener() {  
                public void onClick(View v) {    
                	Intent i=new Intent();
                	i.setClass(UserTabActivity.this, MainActivity.class);
                	i.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                	UserTabActivity.this.startActivity(i);
                	MainActivity.tabhost.setCurrentTab(0);
                	ContextApplication.tabtype=0;
                } 

            }); 
	 }
	  
	 @Override  
	 public boolean onKeyDown(int keyCode,KeyEvent event) {   
	    if (keyCode == KeyEvent.KEYCODE_BACK) { 
//			 new AlertDialog.Builder(UserTabActivity.this).setTitle("提示")
//			 .setMessage("您确定要退出吗？")
//			 .setPositiveButton("确定", new DialogInterface.OnClickListener() {
//				public void onClick(DialogInterface dialog, int whichButton) {
//					android.os.Process.killProcess(android.os.Process.myPid());
//				}
//			  })
//			  .setNegativeButton("取消", new DialogInterface.OnClickListener() {
//				    public void onClick(DialogInterface dialog, int whichButton) {
//				     }
//			  })
//			  .show();
	         return true;   
	     } else
	         return super.onKeyDown(keyCode,event);   
	 }

	public void onTabChanged(String tabId) {	 
    	TextView title=(TextView) UserTabActivity.this.findViewById(R.id.title); 
    	
    	if(tabId.equals("home")){ 
    		ContextApplication.prePage=0;
    		title.setText("首页");
    		right.setVisibility(View.INVISIBLE);
        }
    	if(tabId.equals("usercenter")){
        	ContextApplication.prePage=1;
        	right.setVisibility(View.VISIBLE);
           	title.setText("我的生意街");
           	 
        }
        if(tabId.equals("attention")){
        	ContextApplication.prePage=2;
        	title.setText("关注项目");
        	right.setVisibility(View.INVISIBLE);
        	 
        }
        if(tabId.equals("message")){
        	 ContextApplication.prePage=3;
        	 app.right.setVisibility(View.INVISIBLE);
          	 title.setText("留言项目"); 
          	right.setVisibility(View.INVISIBLE);
        }
        
        if(tabId.equals("exit")){ 
        	 app.right.setVisibility(View.INVISIBLE);
          	 title.setText("退出"); 
          	right.setVisibility(View.INVISIBLE);
       }
    }    
	 
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 1) { 
            if(resultCode == RESULT_OK){
            	 
            } 
        } 
    }

	 protected void onDestroy() {	  
	       super.onDestroy(); 
	 }	
	  
}

	    
